<?php include("/includes/header_print.php") ?>
<?php $idfo = base64_decode($_GET[trim(base64_encode("id"), '=')]); ?>
<?php
	$sql = "SELECT * FROM front_office WHERE id = '".$idfo."'";
	$query = mysql_query($sql);
	$data = mysql_fetch_array($query);
	$data = validateData($data);
	extract($data);

	// SELECTING TRAVEL AGEN
	$sql_select_agent = "SELECT * FROM agent WHERE id = '".$idagent."'";
	$query_select_agent = mysql_query($sql_select_agent);
	$data_agent = mysql_fetch_array($query_select_agent);

	// SELECTING SERVICE CHARGE
	$sql_select_service_charge = "SELECT * FROM service_charge WHERE idfront_office = '".$idfo."'";
	// echo $sql_select_service_charge;
	$query_select_service_charge = mysql_query($sql_select_service_charge);

	// SELECTING TAX SERVICE
	$sql_select_tax_service = "SELECT * FROM setting";
	$query_select_tax_service = mysql_query($sql_select_tax_service);
	$data_tax_service = mysql_fetch_array($query_select_tax_service);
	$tax_service = $data_tax_service["tax"]+$data_tax_service["service"];

	// SELECTING ROOM INFORMATION
	$data_room = getRoom($idroom);

	// CALCULATE THE DATE BETWEEN CHECKOUT AND CHECKIN
	$diff = abs(strtotime($checkout) - strtotime($checkin));
	$days = floor($diff / (60*60*24));
	// $months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
	// $days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));

	// printf("%d years, %d months, %d days\n", $years, $months, $days);
	$grand_total = $data_room["real_paid"] * $days;
?>
<div>
	<div class="pull-left">
		<img src="../img/logo-pn.png" style="height: 40px;">
		<p>Lembongan Island - Bali</p>
		<p><small>purinusa@hotmail.com +62 366 559 63 72</small></p>
	</div>
	<div class="pull-right">
		<div style="font-weight: normal; text-align: right; font-size: 10px"><?php echo date("Y-m-d h:i:s"); ?></div>
		<p class="al_right"><strong>INVOICE</strong></p>
	</div>
	<div class="clear"></div>
</div>
<table>
	<thead>
		<tr>
			<th class="al_left" width="40%">Title</th>
			<th class="al_center" width="20%">Price</th>
			<th class="al_center" width="10%">Guests</th>
			<th class="al_center" width="10%">Quantity</th>
			<th class="al_center" width="20%">Amount</th>
		</tr>
	</thead>
	<tbody id="invoice">
		<tr>
			<td class="al_left">
				<h5><?php echo $data_room["nama"]; ?></h5>
				<span><?php echo date(  "F j, Y", strtotime( $checkin ) ); ?> - <?php echo date(  "F j, Y", strtotime( $checkout ) ); ?></span>
			</td>
			<td class="al_right currency" valign="middle"><?php echo $real_paid; ?></td>
			<td class="al_center" valign="middle"><?php echo $banyak_orang; ?></td>
			<td class="al_center" valign="middle"><?php echo $days; ?></td>
			<td id="amount_room" class="al_right currency" valign="middle"><?php $grand_total = $real_paid * $days; echo $grand_total; ?></td>
		</tr>
		<?php
			if (mysql_num_rows($query) > 0) {
				$service_charge = 0;
				$total_service_charge = 0;
				$total_service_charge_temp = 0;
				while ($data_service_charge = mysql_fetch_array($query_select_service_charge)) {
					if ($data_service_charge["is_paid"] == 1) {
						$total_service_charge_temp += ($data_service_charge["jumlah"]*$data_service_charge["harga"]);
						$jumlah_service_charge = 0;
					}
					$jumlah_service_charge = $data_service_charge[jumlah];

					$total_service_charge = ($data_service_charge["jumlah"]*$data_service_charge["harga"]);
					$grand_total += $total_service_charge;
		?>
		<tr>
			<td class="al_left" valign="middle"><?php echo $data_service_charge["nama"]; ?></td>
			<td class="al_right currency" valign="middle"><?php echo $data_service_charge["harga"]; ?></td>
			<td class="al_center" valign="middle">-</td>
			<td class="al_center" valign="middle"><?php echo $data_service_charge["jumlah"]; ?></td>
			<td class="al_right currency" valign="middle"><?php echo ($data_service_charge["jumlah"]*$data_service_charge["harga"]); ?></td>
		</tr>
		<?php
					$service_charge += $total_service_charge;
				}
			}
		?>
	</tbody>
	<tfoot>
		<tr>
			<td class="al_center" style="height: 10px;"></td>
			<td class="al_right" colspan="3" valign="middle"></td>
			<td class="al_right currency" valign="middle" ></td>
		</tr>
		<tr>
			<td class="al_center">Front Office</td>
			<td class="al_right" colspan="3" valign="middle">Total:</td>
			<td class="al_right currency" valign="middle" ><?php echo $grand_total; ?></td>
		</tr>
		<tr>
			<td></td>
			<td class="al_right" colspan="3" valign="middle">Tax & Service:</td>
			<td id="tax_service" class="al_right" valign="middle" data-tax="<?php echo $tax_service; ?>"><?php echo $tax_service; ?> %</td>
		</tr>
		<tr>
			<td></td>
			<td class="al_right" colspan="3" valign="middle">Grand Total:</td>
			<?php $grand_total_with_tax = ($grand_total+($grand_total*$tax_service/100)); ?>
			<td id="grand_total" class="al_right currency" valign="middle"><?php echo $grand_total_with_tax; ?></td>
		</tr>
		<?php if ($data["is_paid"] == "1") { ?>
			<?php  
				$is_paid = 0;
				$is_paid += ($days * $data_room["harga"]);
				$is_paid += $service_charge;
				$is_paid += $is_paid * $tax_service/100;
				$grand_total = $grand_total_with_tax - $is_paid;
			?>
			<tr>
				<td class="al_right" colspan="4" valign="middle">
					Paid:
				</td>
				<td id="total_paid" class="al_right currency" valign="middle" ><?php echo $is_paid; ?></td>
			</tr>
			<tr>
				<td class="al_right" colspan="4" valign="middle">
					Remaining Payment:
				</td>
				<td id="grand_total" class="al_right currency" valign="middle" ><?php echo $grand_total; ?></td>
			</tr>
		<?php } ?>
		<?php
			switch ($_GET["payment_id"]) {
				case '1':
		?>
		<tr>
			<td></td>
			<td class="al_right" colspan="3" valign="middle">Payment by Cash:</td>
			<td id="grand_total" class="al_right currency" valign="middle"><?php echo $_GET["payment_cash"]; ?></td>
		</tr>
		<tr>
			<td style="border-bottom: 1px solid #CCC;"></td>
			<td class="al_right" colspan="3" valign="middle">Your Change:</td>
			<td id="grand_total" class="al_right currency" valign="middle"><?php echo ($_GET["payment_cash"] - $grand_total); ?></td>
		</tr>
		<?php
					break;
				case '2':
		?>
		<tr>
			<td></td>
			<td class="al_right" colspan="3" valign="middle">Credit Card Charge:</td>
			<td id="grand_total" class="al_right" valign="middle"><?php echo $_GET["credit_tax"]; ?> %</td>
		</tr>
		<tr>
			<td style="border-bottom: 1px solid #CCC;"></td>
			<td class="al_right" colspan="3" valign="middle">Full Payment:</td>
			<td id="grand_total" class="al_right currency" valign="middle"><?php echo $grand_total + ($grand_total * $_GET["credit_tax"] / 100); ?></td>
		</tr>
		<?php
					break;
				case '3':
		?>
		<tr>
			<td></td>
			<td class="al_right" colspan="3" valign="middle">Payment by Voucher
			</td>
			<td id="grand_total" class="al_right currency" valign="middle"><?php echo $_GET["payment_cash"]; ?></td>
		</tr>
		<tr>
			<td style="border-bottom: 1px solid #CCC;"></td>
			<td colspan="3"></td>
			<td></td>
		</tr>
		<?php
					break;
			}
		?>
	</tfoot>
</table>
<?php include("/includes/footer_print.php") ?>
<script>
	$(document).ready(function() {
		window.print();
		// window.onfocus=function(){ window.close();}
	});
</script>